package rdd01.transform

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Spark_GrounpBy {
  def main(args: Array[String]): Unit = {
    val sparkConf=new SparkConf().setMaster("local[*]").setAppName("FlatMap")
    val sc=new SparkContext(sparkConf)
    val rdd:RDD[Int] = sc.makeRDD(List(1, 2, 3, 4), 2)
    //GroupBy算子
    def groupByfunction(num:Int):Int={
      num % 2
    }
    val groupRDD: RDD[(Int, Iterable[Int])] = rdd.groupBy(groupByfunction)
     groupRDD.collect().foreach(println)
    //或根据首字目来区分
    val rdd1=sc.makeRDD(List("Hello","Spark","Hcala","Hadoop"),2)
    val groupRDD1: RDD[(Char, Iterable[String])] = rdd1.groupBy(_.charAt(0))
    groupRDD1.collect().foreach(println)
    sc.stop()
  }

}
